package com.sunyur.tailor.channel.web.task.user;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.common.middle.util.DateUtil;
import com.sunyur.tailor.channel.service.middle.user.MiddleUserService;
import com.sunyur.tailor.channel.service.outer.user.param.OuterUserParam;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/***
 * @Author xuhui
 * @Description 人员定时
 * @Date 21:56 2021/6/26

 **/
@Component
@JobHandler("userJobHandler")
public class UserJobHandler extends IJobHandler {

    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(UserJobHandler.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(UserJobHandler.class);

    @Autowired
    private MiddleUserService middleUserService;

    @Override
    public ReturnT<String> execute(String param) throws Exception {
        LOGGER_INFO.info("UserJobHandler:{}", param);
        XxlJobLogger.log("UserJobHandler:{}", param);
        if (StringUtils.isEmpty(param)){
            return ReturnT.FAIL;
        }
        long hrStartDate = DateUtil.dateToUnixTimestamp();
        //调用组织处理接口
        OuterUserParam outerUserParam = JSON.parseObject(param, OuterUserParam.class);
        ChannelResult channelResult = middleUserService.syncUser(outerUserParam);
        long hrEndDate = DateUtil.dateToUnixTimestamp();
        long diff = DateUtil.diffSecond(hrStartDate, hrEndDate);
        LOGGER_INFO.info("UserJobHandler定时任务结束,耗时:{},结果：{}", diff, JSON.toJSONString(channelResult));
        XxlJobLogger.log("UserJobHandler定时任务结束,耗时:{},结果：{}", diff, JSON.toJSONString(channelResult));
        if (ChannelResultUtil.isSuccess(channelResult)) {
            return IJobHandler.SUCCESS;
        }
        return IJobHandler.FAIL;
    }
}
